package imp.core.data;

import imp.core.basics.IOManager;
import imp.core.data.configuration.FilePaths;
import imp.core.data.songdata.SongData;
import imp.core.misc.SortManager;

import java.util.ArrayList;


/**
 * The Category Data Class contains the categories which can be assigned to a song.
 * @author Patrick Arnold
 * @since 09.09.2010
 *
 */
public class CategoryData implements FilePaths {

	/** The list of categories. */
	private static ArrayList<String> categories;
	
	
	/**
	 * Loads the categories and initializes the list of categories.
	 * @return Specifies whether loading the categories has been successful or not.
	 */
	public static boolean loadData() {
		
		String path = PathData.getProgrampath() + CATEGORIES;
		String[] file = IOManager.loadFile( path);
		
		if( file == null) {
			return false;
		}
		
		for( int i=0; i < file.length; i++) {
			String category = file[i];
			category = category.trim();
			categories.add( category);
		}
		
		
		return true;
		
	}
	
	
	
	/**
	 * Writes the categories.
	 */
	public static void writeData() {
		
		int l = categories.size();
		String output[] = new String[l];
		
		for( int i=0; i < l; i++) {
			output[i] = categories.get(i);
		}
		
		String path = PathData.getProgrampath() + CATEGORIES;
		IOManager.write( output, path, false);
	
	}
	
	
	
	/**
	 * Adds a new category in the list of categories.
	 * @param category The new category.
	 */
	public static void addCategory( String category) {
		categories.add( category);
		categories = SortManager.sortArraylist( categories);
	}
	
	
	
	/**
	 * Deletes a category.
	 * @param category The name of the category which is to be deleted.
	 * @param replacement The replacement for all songs which have the category which is to
	 * be deleted.
	 */
	public static void deleteCategory( String category, String replacement) {
		
		for( int i=0; i < categories.size(); i++) {
			if( categories.get(i).equals( category)) {
				categories.remove(i);
			}
		}
		
		SongData.changeCategory( category, replacement);
		
	}
	
	
}